#coding:utf-8
from lovewith.website.models import Color
from lovewith.utils.mtcache import MtCache


#颜色
class ColorApi:
    def __init__(self):
        pass

    @staticmethod
    def check_data(color_value=None, color_id=None):
        color_data = None
        if color_value:
            color_data = Color.objects.filter(value='#%s'% color_value)
        if color_id:
            color_data = Color.objects.filter(id=int(color_id))

        if color_data and color_data.exists():
            return color_data[0]
        else:
            return False

    #全部颜色
    @staticmethod
    def get_all():
        color_data = MtCache('color').get()

        if not color_data:
            color_data = []
            color_filter_data = Color.objects.all().order_by('-sort')
            for color in color_filter_data:
                color_data.append({'id': color.id, 'name': color.name, 'color': color.value})

            MtCache('color').set(color_data)

        return color_data





















